//
//  BKSayToTaViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/27.
//  Copyright (c) 2015年 报考大学. All rights reserved.
//

#import "BKSayToTaViewController.h"
#import "BKTextView.h"
#import "BKTextField.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKSayTaViewModel.h"
#import "LoginViewController.h"
@interface BKSayToTaViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet BKTextView *content;
@property (weak, nonatomic) IBOutlet BKTextField *date;
@property (weak, nonatomic) IBOutlet BKTextField *taphone;
@property (weak, nonatomic) IBOutlet BKTextField *wophone;
@property (weak, nonatomic) IBOutlet BKTextField *womingzi;
@property (weak, nonatomic) IBOutlet UISwitch *isEnable;

@end

@implementation BKSayToTaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [_content setTextColor:[UIColor whiteColor]];
    
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    datePicker.locale = locale;

    datePicker.datePickerMode =UIDatePickerModeDateAndTime;
    _date.inputView = datePicker;
    
    UIToolbar *accessoryView = [[UIToolbar alloc] init];
    accessoryView.frame=CGRectMake(0, 0, 320, 38);
    accessoryView.backgroundColor = [UIColor grayColor];
    UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    @weakify(self);
    doneBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        self.date.text = [formatter stringFromDate:datePicker.date];
        
        [self.date endEditing:YES];
        return [RACSignal empty];
    }];

    [doneBtn setTintColor:[UIColor blackColor]];
    UIBarButtonItem *spaceBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    accessoryView.items=@[spaceBtn,doneBtn];
    _date.inputAccessoryView = accessoryView;
    _isEnable.transform = CGAffineTransformScale(CGAffineTransformIdentity, .8, .8);
    [_isEnable setOnTintColor:[UIColor colorWithRed:0.055 green:0.216 blue:0.235 alpha:1.000]];
    [_isEnable setOn:NO];
    
    
}
- (IBAction)p_submit:(id)sender {
    
    
    
    if ([_nameTextField.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写对方的名字" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    if ([_content.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写要说的内容" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    if ([_date.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择日期" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    if ([_taphone.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写他/她的手机号码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    if ([_wophone.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写自己的手机" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    if ([_womingzi.text length]==0) {
        UIAlertView *alertView =  [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写自己的名字" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        
        [alertView show];
        return;
    }
    
    NSString *niming = @"2";
    if (_isEnable.on) {
        niming = @"1";
    }
    
  
    NSArray *dateTime  = [_date.text componentsSeparatedByString:@" "];
    if ([dateTime count]!=2) {
        return;
    }
    
    NSArray *date = [dateTime[0] componentsSeparatedByString:@"-"];
    NSArray *time = [dateTime[1] componentsSeparatedByString:@":"];
    
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults objectForKey:@"Cookie"] !=nil) {
    
        [BKSayTaViewModel sayToWithtaname:_nameTextField.text content:_content.text taphone:_taphone.text phone:_wophone.text name:_womingzi.text niming:niming year:date[0] yue:date[1] ri:date[2] shi:time[0] success:^{
            
            dispatch_queue_t mainQueue = dispatch_get_main_queue();
            dispatch_async(mainQueue, ^(void) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存成功！" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                [alertView show];
                
            });
        } failure:^(NSString *message) {
            dispatch_queue_t mainQueue = dispatch_get_main_queue();
            dispatch_async(mainQueue, ^(void) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                [alertView show];
            });
            
        }];
    }else{
    
    
        LoginViewController *login = [[LoginViewController alloc] initWithNibName:NSStringFromClass([LoginViewController class]) bundle:nil];
        [self presentViewController:login animated:YES completion:nil];

        
    }
    
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [_nameTextField resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
